﻿using System;
using System.Linq;
using MvcMall.AdminWeb.DtoModels;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Framework.MvcPager;

namespace MvcMall.AdminWeb.Manage.AdminPowers
{
    public partial class Index : ManageBasePage
    {
        private IAdminPowerTask _adminPowerTask;
        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("AdminPowerIndex");
            _adminPowerTask = GetInstance<IAdminPowerTask>();

            if (!IsPostBack)
            {
                BindRpt();
            }
        }

        private void BindRpt()
        {
            var list = _adminPowerTask.GetPagedList(ANPager.CurrentPageIndex, ANPager.PageSize);
            var dtoList = list.Select(m => new AdminPowerDto
                                               {
                                                   Id = m.Id,
                                                   ModulName = m.AdminModul.ModulName,
                                                   PowerCode = m.PowerCode,
                                                   PowerName = m.PowerName,
                                               })
                .ToPagedList(ANPager.CurrentPageIndex, ANPager.PagesRemain, list.TotalItemCount);
            rptPowers.DataSource = dtoList;
            rptPowers.DataBind();
            ANPager.RecordCount = dtoList.TotalItemCount;
        }

        protected void ANPager_PageChanged(object sender, EventArgs e)
        {
            BindRpt();
        }
    }
}